# set working directory
setwd('~/replication_files')

# function to load and install packages
load_packages<-function(packages){
  sapply(packages,function(x){
    f<-require(x,character.only=T)
    if(!f) install.packages(x); f<-require(x,character.only=T)
    return(f)},USE.NAMES=T)
}

# load packages
load_packages(c('data.table','ggplot2','gridExtra'))

# function to extract legend from ggplot object
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

(plotDT<-readRDS('Figure2.rds'))

# create data.table of referred estimates
(pdt1<-plotDT[ref_type=='prev',list(site_type,referrer,pct_referred=value)])
# create data.table of referral estimates
(pdt2<-plotDT[ref_type=='next',list(site_type,referrer,pct_referred=value)])

# subset both to only include FB, Google, or mail
(pdt1<-pdt1[referrer%in%c('Facebook','Google','Webmail')])
(pdt2<-pdt2[referrer%in%c('Facebook','Google','Webmail')])

# create a legend
p1.leg<-ggplot(data=pdt1,mapping=aes(x=referrer,y=pct_referred,color=site_type,fill=site_type))+
  geom_col(position='dodge')+
  scale_color_manual(values=c('gray80','gray40','gray10'))+
  scale_fill_manual(values=c('gray80','gray40','gray10'))+
  guides(fill=guide_legend(title="Webpage type"),color=F)+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),panel.grid.major.y=element_line(color='gray90'),
                   panel.grid.minor=element_blank(),axis.text=element_text(color='black'),legend.position='bottom')

# plot of % visiting one of these sites before exposure
p1<-ggplot(data=pdt1,mapping=aes(x=referrer,y=pct_referred,color=site_type,fill=site_type))+
  geom_col(position='dodge')+
  scale_color_manual(values=c('gray80','gray40','gray10'))+
  scale_fill_manual(values=c('gray80','gray40','gray10'))+
  labs(x=NULL,y=NULL,title='Before exposure')+
  guides(fill=F,color=F)+
  scale_y_continuous(labels=function(x) paste0(x,"%"),breaks=seq(0,12,3))+
  coord_cartesian(ylim=c(0,12))+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),panel.grid.major.y=element_line(color='gray90'),
                   plot.title=element_text(hjust=0.5),panel.grid.minor=element_blank(),axis.text=element_text(color='black'),axis.text.x=element_text(margin=margin(-8,0,0,0)))

# plot of % visiting one of these sites after exposure
p2<-ggplot(data=pdt2,mapping=aes(x=referrer,y=pct_referred,color=site_type,fill=site_type))+
  geom_col(position='dodge')+
  scale_color_manual(values=c('gray80','gray40','gray10'))+
  scale_fill_manual(values=c('gray80','gray40','gray10'))+
  labs(x=NULL,y=NULL,title='After exposure')+
  guides(fill=F,color=F)+
  scale_y_continuous(labels=function(x) paste0(x,"%"),breaks=seq(0,12,3))+
  coord_cartesian(ylim=c(0,12))+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),panel.grid.major.y=element_line(color='gray90'),
                   plot.title=element_text(hjust=0.5),panel.grid.minor=element_blank(),axis.text=element_text(color='black'),axis.text.x=element_text(margin=margin(-8,0,0,0)))

# arrange together
p<-arrangeGrob(arrangeGrob(p1,p2,nrow=1),g_legend(p1.leg),ncol=1,heights=c(1,.08))

# save plot
ggsave('Figure2.pdf',p,width=8,height=6,units='in',dpi=300)
